/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.full.command;

import java.util.ArrayList;
import java.util.ListIterator;

import javax.swing.JInternalFrame;

import br.ufmg.dees.insane.commons.command.Command;
import br.ufmg.dees.insane.commons.controller.Controller;
import br.ufmg.dees.insane.commons.dialogs.TabbedDialog;
import br.ufmg.dees.insane.commons.view.DrawingArea;
import br.ufmg.dees.insane.model.disc.element.FrameElement;
import br.ufmg.dees.insane.model.disc.pointforce.PointForce;
import br.ufmg.dees.insane.ui.rich.full.InsaneInternalInterface;
import br.ufmg.dees.insane.ui.rich.full.dialogs.ElmConcForcesDialog;

/**
 *
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @since April 2004
 */
public class SetElmConcForcesCommand implements Command, Cloneable {
	
	private InsaneInternalInterface parent;
	private Controller controller;
	private DrawingArea da;
	
//*****************************************************************************
	
	/** Creates new SetElmConcForcesCommand */
	public SetElmConcForcesCommand(JInternalFrame parent) {
		this.parent = (InsaneInternalInterface)parent;
		controller = this.parent.getController();
		da = this.parent.getDrawing();
	}
	
//*****************************************************************************
	
	/** Execute method. */
	public void execute() {
		
		boolean changed = false;
		
		ArrayList elms = controller.getSelection();
		if ( elms.size() < 1 ) {
			controller.selectAll();
			elms = controller.getSelection();
		}
		
		if (elms.size() == 0) {
			parent.addOutputText("There is no elements in model.");
			return;
		}
		
        ElmConcForcesDialog dlg = new ElmConcForcesDialog(parent);
        int retVal = dlg.showDialog();
        if (retVal == TabbedDialog.OK_OPTION){
			
			ListIterator elements = elms.listIterator();
			while(elements.hasNext()) {
				FrameElement elm = (FrameElement)elements.next();
				
				if (elm.getElmPointForcesList().size() != dlg.concForces.size()) {
					changed = true;
				} else {
					ListIterator cfs1 = elm.getElmPointForcesList().listIterator();
					ListIterator cfs2 = dlg.concForces.listIterator();
					while (cfs1.hasNext()) {
						PointForce cf1 = (PointForce)cfs1.next();
						PointForce cf2 = (PointForce)cfs2.next();
						if (cf1.getCoord().x != cf2.getCoord().x)
							changed = true;
						if (cf1.getForce(0) != cf2.getForce(0))
							changed = true;
						if (cf1.getForce(1) != cf2.getForce(1))
							changed = true;
						if (cf1.getForce(2) != cf2.getForce(2))
							changed = true;
						if (cf1.getForce(3) != cf2.getForce(3))
							changed = true;
						if (cf1.getForce(4) != cf2.getForce(4))
							changed = true;
						if (cf1.getForce(5) != cf2.getForce(5))
							changed = true;
					}
				}
				
				//Setting ElmPointForcesList
				ArrayList<PointForce> cforces = new ArrayList<PointForce>();
				ListIterator cfs = dlg.concForces.listIterator();
				while (cfs.hasNext()) {
					PointForce cf = (PointForce)cfs.next();
					PointForce cf2 = new PointForce();
					if (cf.getCoord().x > elm.getLength()){
						cf2.getCoord().x = elm.getLength();
					} else {
						cf2.setCoord(cf.getCoord());
					}
					cf2.setForce(0, cf.getForce(0));
					cf2.setForce(1, cf.getForce(1));
					cf2.setForce(2, cf.getForce(2));
					cf2.setForce(3, cf.getForce(3));
					cf2.setForce(4, cf.getForce(4));
					cf2.setForce(5, cf.getForce(5));
					cforces.add(cf2);
				}
				elm.setElmPointForcesList(cforces);
				
			}
			
		}
		
		//parent.addToUndo((Command)this.clone());
		controller.unSelectAll();
		controller.compose();
		da.repaint();
		
		if (changed)
			parent.cleanResults();
		
	}
	
//*****************************************************************************
	
	/** Impossible to undo - not implemented. */
	public void undo() {
	}
	
//*****************************************************************************
	
	/** Impossible to redo - not implemented. */
	public void redo() {
	}
	
//*****************************************************************************
	
	/** Clone method. */
	public Object clone() {
		try {
			SetElmConcForcesCommand cmd = (SetElmConcForcesCommand)super.clone();
			return cmd;
		}catch (CloneNotSupportedException e) {
			throw new Error("Clonning ERROR - This should never happen!");
		}
	}
	
//*****************************************************************************
}

